<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库位上级" prop="parentId">
        <el-select v-model="queryParams.parentId" placeholder="请选择库位上级" clearable>
          <el-option
              v-for="dict in sys_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入仓库名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker
            v-model="daterangeUpdateTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['wms:warehouse:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="Sort"
            @click="toggleExpandAll"
        >展开/折叠
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
        v-if="refreshTable"
        v-loading="loading"
        :data="warehouseList"
        row-key="id"
        :default-expand-all="isExpandAll"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="上级" align="center" prop="parentId"/>
      <el-table-column label="仓库名称" align="center" prop="name"/>
      <el-table-column label="位置" align="center" prop="location"/>
      <el-table-column label="面积" align="center" prop="area"/>
      <el-table-column label="负责人" align="center" prop="userId"/>
      <el-table-column label="物品列表" align="center" prop="goodsList"/>
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template #default="scope">
          <dict-tag :options="sys_state" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['wms:warehouse:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['wms:warehouse:add']">
            新增
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['wms:warehouse:remove']">删除
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <!--      <el-table-column label="创建者" align="center" prop="createBy" />-->
      <!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
      <!--        <template #default="scope">-->
      <!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="更新者" align="center" prop="updateBy" />-->
      <!--      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">-->
      <!--        <template #default="scope">-->
      <!--          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <!-- 添加或修改仓库对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="warehouseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库位上级" prop="parentId">
          <el-tree-select
              v-model="form.parentId"
              :data="warehouseOptions"
              :props="{ value: 'id', label: 'name', children: 'children' }"
              value-key="id"
              placeholder="请选择库位上级"
              check-strictly
          />
        </el-form-item>
        <el-form-item label="仓库名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入仓库名称"/>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积"/>
        </el-form-item>
        <el-form-item label="负责人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入负责人"/>
        </el-form-item>
        <el-form-item label="物品列表" prop="goodsList">
          <el-input v-model="form.goodsList" placeholder="请输入物品列表"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="enableFlag">
          <el-select v-model="form.enableFlag" placeholder="请选择是否启用">
            <el-option
                v-for="dict in sys_state"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Warehouse">
import {listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse} from "@/api/wms/warehouse";

const {proxy} = getCurrentInstance();
const {sys_state} = proxy.useDict('sys_state');

const warehouseList = ref([]);
const warehouseOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);
const daterangeUpdateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    parentId: null,
    name: null,
    updateTime: null,
  },
  rules: {
    parentId: [
      {required: true, message: "库位上级不能为空", trigger: "change"}
    ],
    name: [
      {required: true, message: "仓库名称不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询仓库列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0];
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1];
  }
  listWarehouse(queryParams.value).then(response => {
    warehouseList.value = proxy.handleTree(response.data, "id", "parentId");
    loading.value = false;
  });
}

/** 查询仓库下拉树结构 */
function getTreeselect() {
  listWarehouse().then(response => {
    warehouseOptions.value = [];
    const data = {id: 0, name: '顶级节点', children: []};
    data.children = proxy.handleTree(response.data, "id", "parentId");
    warehouseOptions.value.push(data);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentId: null,
    ancestors: null,
    name: null,
    location: null,
    area: null,
    userId: null,
    goodsList: null,
    enableFlag: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    tenantId: null
  };
  proxy.resetForm("warehouseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeUpdateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加仓库";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.id;
  }
  getWarehouse(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改仓库";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["warehouseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarehouse(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarehouse(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除仓库编号为"' + row.id + '"的数据项？').then(function () {
    return delWarehouse(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

getList();
</script>
